package com.yc.sole.system.controller;

import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.RoleBiz;
import com.yc.sole.system.request.RoleMenuReq;
import com.yc.sole.system.request.RoleQueryReq;
import com.yc.sole.system.request.RoleReq;
import com.yc.sole.system.response.RoleResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

/**
 * 角色管理
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class RoleController {

    private final RoleBiz roleBiz;

    /**
     * 新增
     *
     * @param roleReq
     * @return RestResult
     */
    @PreAuth(value = "system:role:save", description = "新增角色")
    @PostMapping("/api/system/role/saveRole")
    public Result<Void> saveRole(@RequestBody @Validated(SaveGroup.class) RoleReq roleReq) {
        roleBiz.saveRole(roleReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param roleReq
     * @return RestResult
     */
    @PreAuth(value = "system:role:update", description = "修改角色")
    @PostMapping("/api/system/role/updateRoleById")
    public Result<Void> updateRoleById(@RequestBody @Validated(UpdateGroup.class) RoleReq roleReq) {
        roleBiz.updateRoleById(roleReq);
        return Result.success();
    }

    /**
     * 分配权限
     *
     * @param roleMenuReq
     * @return RestResult
     */
    @PreAuth(value = "system:role:update", description = "分配权限")
    @PostMapping("/api/system/role/updateRolePermission")
    public Result<Void> updateRolePermission(@RequestBody @Validated(UpdateGroup.class) RoleMenuReq roleMenuReq) {
        roleBiz.updateRolePermission(roleMenuReq);
        return Result.success();
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "system:role:delete", description = "删除角色")
    @PostMapping("/api/system/role/deleteRole")
    public Result<Void> deleteRole(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        roleBiz.deleteRole(baseDeleteReq.getIds());
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "system:role:select", description = "分页查询角色")
    @PostMapping("/api/system/role/selectRolePage")
    public Result<Paging<RoleResp>> selectRolePage(@RequestBody @Validated BasePageReq<RoleQueryReq> basePageReq) {
        return Result.data(roleBiz.selectRolePage(basePageReq.toPage(), basePageReq.getQuery()));
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return RestResult
     */
    @PreAuth(value = "system:role:select", description = "查询角色")
    @PostMapping("/api/system/role/{id}")
    public Result<RoleResp> selectRoleById(@PathVariable Long id) {
        return Result.data(roleBiz.selectRoleById(id));
    }

    /**
     * 查询角色拥有菜单id
     *
     * @param id
     * @return RestResult
     */
    @PreAuth(value = "system:role:select", description = "查询角色拥有菜单id")
    @PostMapping("/api/system/role/selectMenuIds/{id}")
    public Result<Collection<Long>> selectMenuIds(@PathVariable Long id) {
        return Result.data(roleBiz.selectMenuIds(id));
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @PreAuth(value = "system:role:select", description = "查询所有角色")
    @PostMapping("/api/system/role/selectRoleList")
    public Result<List<RoleResp>> selectRoleList() {
        return Result.data(roleBiz.selectRoleList());
    }
}
